package com.codeyang.mocktest2.mockit;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import java.util.List;

// 这个相当于把mock注解初始化mock对象的操作完成
@RunWith(MockitoJUnitRunner.class)
public class MockitoTest03 {
    @Mock
    private List mockList;

    @Test
    public void shorthand() {
//        List<String> mock = Mockito.mock(List.class);  相当于被注解替换了
        Mockito.doAnswer(new MockitoTest03.CustomAnswer2()).when(mockList).get(Mockito.anyInt());
        Assert.assertEquals("大于三", mockList.get(4));
        Assert.assertEquals("小于三", mockList.get(2));
        //TDD  输出预期改变值
        System.out.println(mockList.get(2));
    }

    public class CustomAnswer2 implements Answer<String> {
        public String answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            Integer num = (Integer) args[0];
            if (num > 3) {
                return "大于三";
            } else {
                return "小于三";
            }
        }
    }
}